package com.cellulam.framework.scaffold.generate.db;

import com.cellulam.framework.scaffold.generate.utils.GenUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

public class JdbcConfig {

    private static final Map<String, JdbcConfig> pool = new HashMap<>();

    private String jdbcDriver;
    private String jdbcUrl;
    private String user;
    private String password;
    private String dbName;
    private String dbPackageName;

    public JdbcConfig(String jdbcDriver, String jdbcUrl, String dbName, String dbPackageName, String user, String password) {
        this.jdbcDriver = jdbcDriver;
        this.jdbcUrl = jdbcUrl;
        this.user = user;
        this.password = password;

//        String dbName = jdbcUrl.contains("?") ? jdbcUrl.substring(0, jdbcUrl.indexOf('?')) : jdbcUrl;
//        this.dbName = dbName.substring(dbName.lastIndexOf('/')+1);
        this.dbName = dbName;
        this.dbPackageName = dbPackageName;

        pool.put(this.dbName, this);
    }

    private List<String> tableNames;

    public static JdbcConfig parse(Class<?> clazz, String jdbcResourcePath) throws IOException {
        Properties loadedProp = GenUtils.loadJdbcProp(clazz, jdbcResourcePath);

        String usedDatabase = loadedProp.getProperty("used.database");
        String packageNames = loadedProp.getProperty("databse.package.names");
        Map<String, String> dbPackageNameMap = Arrays.stream(packageNames.split(","))
                .map(kv -> kv.split(":"))
                .collect(Collectors.toMap(kv -> kv[0], kv -> kv[1]));
        String dbPackageName = dbPackageNameMap.get(usedDatabase);
        if (StringUtils.isBlank(dbPackageName)) {
            throw new RuntimeException("db[" + usedDatabase + "]'s package name is null");
        }
        String tableNameText = loadedProp.getProperty("generate.table.names");

        JdbcConfig config = GenUtils.createJdbcProp(loadedProp, usedDatabase, dbPackageName);

        config.tableNames = Arrays.stream(tableNameText.split(","))
                .filter(StringUtils::isNotBlank)
                .map(StringUtils::trimToEmpty)
                .collect(Collectors.toList());

        return config;
    }

    public List<String> getTableNames() {
        return tableNames;
    }

    public static JdbcConfig get(String dbName) {
        return pool.get(dbName);
    }

    public String getDbName() {
        return dbName;
    }

    public String getDbNameNoLine() {
        return dbName.replace("_", "").replace("-", "");
    }

    public String getJdbcUrl() {
        return jdbcUrl;
    }

    public void setJdbcUrl(String jdbcUrl) {
        this.jdbcUrl = jdbcUrl;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getJdbcDriver() {
        return jdbcDriver;
    }

    public void setJdbcDriver(String jdbcDriver) {
        this.jdbcDriver = jdbcDriver;
    }

    public String getDbPackageName() {
        return dbPackageName;
    }

    public void setDbPackageName(String dbPackageName) {
        this.dbPackageName = dbPackageName;
    }
}
